package com.qadev.appergodic.utils;

import com.qadev.appergodic.bean.XpathValue;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.testng.annotations.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wjp on 2019/8/7.
 */
public class XmlUtils {
    //解析xml文件获取元素
    @Test
    public void getXpath() {
        List<XpathValue> xpathValueList = new ArrayList<XpathValue>();
        try {
//            //   .ui.FirstWelcomeActivity1565601391017.xml .ui.home.HomeActivity1565601406535.xml
//            File file = new File("D:\\zk\\V3.2\\.ui.FirstWelcomeActivity1565601391017.xml");
//            SAXReader reader = new SAXReader();
//            Document doc = reader.read(file);
//            Element root=doc.getRootElement();
////            String xpath ="/";
//            Iterator<Element> it = root.elementIterator();
//            if (it.hasNext() ) {
//                Element element=it.next();
//                // do something
//                selectElement(element,xpathValueList);
//            }
//            System.out.println(xpathValueList.size());
//            for (XpathValue xv:xpathValueList
//                 ) {
//                System.out.println(xv.toString());
//            }
            String filepath="E:/Doumi/TestDoumi/.activity.common.login.OtherChannelLogInActivity1570852780577.xml";
            getXmlValue(filepath,xpathValueList);
            for (XpathValue xv:xpathValueList
                 ) {
                System.out.println(xv.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//    return xpathValueList;

    }

    /**
     *
     * @param filepath
     * @param xpathValueList
     * @return
     */
    public void getXmlValue(String filepath,List<XpathValue> xpathValueList){
        Iterator<Element> it = readXmlRoot(filepath).elementIterator();
        if (it.hasNext()) {
            Element element=it.next();
            // do something
            selectElement(element,xpathValueList);
        }
    }

    /**
     * 读取xml文件
     * @param filepath xml文件地址
     * @return
     */
    public Element readXmlRoot(String filepath){
        Element element=null;
        try {
            File file = new File(filepath);
            SAXReader reader = new SAXReader();
            Document doc = reader.read(file);
            element=doc.getRootElement();
        }catch (Exception e){
            e.printStackTrace();
        }
        return element;
    }

    /**现在是获取某个元素的子节点出现了问题
     * 当有子节点时循环时元素应该为父节点当为参数
     *
     * 当做单个节点的循环
     * 判断节点的节点数量，如果有一个节点则直接循环，如果自己点大于1个则循环
     */
    private void getElementList(Element element,List<XpathValue> xpathValueList){
        //先判断当前节点下子节点的个数,如果当前节点下的子节点只有一个则直接调用循环getElement
        //如果当前节点下的子节点不是1个，那么在调用时传递getElements
        //重新调用getElementList
        System.out.println("测试长度:"+element.nodeCount()+"标签name:"+element.getName());
        Iterator<Element> iterator=element.elementIterator();
        Element e=null;
        if(element.nodeCount()==1){
            e=iterator.next();
            getElement(element,xpathValueList);
        }else {
            while (iterator.hasNext()) {
                Element element1 = iterator.next();
                if (element1.nodeCount() == 1) {
                    getElement(element1, xpathValueList);
                } else {
                    getElements(element1, xpathValueList);
                }
                e=element1;
            }

        }
        //传入下个节点的节点名称
        if (e!=null){
            getElementList(e, xpathValueList);
        }
    }

    /**
     * 子节点循环出来全部的数据
     * @param element
     * @param xpathValueList
     */
    private static void getElements(Element element,List<XpathValue> xpathValueList) {
        //获取父节点
        getElement(element, xpathValueList);
        XpathValue xpathValue = new XpathValue();
        //在添加子节点
        Iterator<Element> iterator = element.elementIterator();
        while (iterator.hasNext()) {
            addList(iterator,xpathValueList,xpathValue);
        }
    }

    /**
     * 将xml元素添加到xpathvalue中，返回list
     * @param element
     * @param xpathValueList
     */
    private static void getElement(Element element,List<XpathValue> xpathValueList){
        //元素中的clickable属性
        System.out.println("xml元素中的clickable的属性为:"+element.attributeValue("clickable"));
        if (element.attributeValue("clickable").equals("true")){
            XpathValue xpathValue =new XpathValue();
            xpathValue.setClass_(element.attributeValue("class"));
            xpathValue.setIndex(element.attributeValue("index"));
            xpathValue.setResource_id(element.attributeValue("resource-id"));
            xpathValue.setText(element.attributeValue("text"));
            xpathValue.setInstance(element.attributeValue("instance"));
            xpathValueList.add(xpathValue);
        }
                 // TODO: 2019/8/14 其他类型的
     }

    private static void addList(Iterator<Element> iterator,List<XpathValue> xpathValueList,XpathValue xpathValue){
            Element element1 = iterator.next();
            xpathValue.setClass_(element1.attributeValue("class"));
            xpathValue.setIndex(element1.attributeValue("index"));
            xpathValue.setResource_id(element1.attributeValue("resource-id"));
            xpathValue.setText(element1.attributeValue("text"));

//            if (element.attributeValue("clickable").equals("true")){
            xpathValueList.add(xpathValue);

    }

    /**
     * 获取xml文件的节点
     * @param element
     * @param xpathValueList
     */
    private void selectElement(Element element,List<XpathValue> xpathValueList){
        if(element.elements().size()>=1){
            List<Element> elements =element.elements();
            getElement(element,xpathValueList);
            for (Element e:elements
                 ) {
                selectElement(e,xpathValueList);
            }
        }else {
            getElement(element,xpathValueList);

        }
    }
}
